package com.pig4cloud.pig.model.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.model.api.entity.ManagementEntity;
import com.pig4cloud.pig.model.api.entity.ModelServiceEntity;
import com.pig4cloud.pig.model.service.ManagementService;
import com.pig4cloud.pig.model.service.ModelServiceService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 模型库管理表
 *
 * @author xuzhuang
 * @date 2024-06-06 14:42:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/management" )
@Tag(description = "management" , name = "模型库管理表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ManagementController {

    private final ManagementService managementService;
    private final ModelServiceService modelService;
	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param management 模型库管理表
	 * @return
	 */
	@Inner(false)
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R getModeManagementPage(@ParameterObject Page page, @ParameterObject ManagementEntity management) {
        LambdaQueryWrapper<ManagementEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(management.getName()), ManagementEntity::getName,management.getName());
		wrapper.like(StrUtil.isNotBlank(management.getStatus()), ManagementEntity::getStatus,management.getStatus());
		wrapper.like(StrUtil.isNotBlank(management.getSortId()), ManagementEntity::getSortId,management.getSortId());
        return R.ok(managementService.page(page, wrapper));
    }


    /**
     * 通过id查询模型库管理表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('management_view')" )
    public R getById(@PathVariable("id" ) Long id) {
		ManagementEntity managementEntity=managementService.getById(id);
		ModelServiceEntity modelServiceEntity=modelService.getById(managementEntity.getServiceId());
		managementEntity.setPath(managementEntity.getUrl());
		managementEntity.setUrl(modelServiceEntity.getProtocol()+"://127.0.0.1:9999"+modelServiceEntity.getPath()+managementEntity.getUrl());
		return R.ok(managementEntity);
    }

    /**
     * 新增模型库管理表
     * @param management 模型库管理表
     * @return R
     */
    @Operation(summary = "新增模型库管理表" , description = "新增模型库管理表" )
    @SysLog("新增模型库管理表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('management_add')" )
    public R save(@RequestBody ManagementEntity management) {

		management.setCreateTime(LocalDateTime.now());
    	management.setCreateBy(SecurityUtils.getUser().getName());
        return R.ok(managementService.save(management));
    }

    /**
     * 修改模型库管理表
     * @param management 模型库管理表
     * @return R
     */
    @Operation(summary = "修改模型库管理表" , description = "修改模型库管理表" )
    @SysLog("修改模型库管理表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('management_edit')" )
    public R updateById(@RequestBody ManagementEntity management) throws MalformedURLException {
    	management.setUpdateTime(LocalDateTime.now());
		management.setUpdateBy(SecurityUtils.getUser().getName());
		URL url = new URL(management.getUrl());
		management.setUrl(url.getPath());
        return R.ok(managementService.updateById(management));
    }

    /**
     * 通过id删除模型库管理表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除模型库管理表" , description = "通过id删除模型库管理表" )
    @SysLog("通过id删除模型库管理表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('management_del')" )
    public R removeById(@RequestBody String[] ids) {
        return R.ok(managementService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param management 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('management_export')" )
    public List<ManagementEntity> export(ManagementEntity management,String[] ids) {
        return managementService.list(Wrappers.lambdaQuery(management).in(ArrayUtil.isNotEmpty(ids), ManagementEntity::getId, ids));
    }
}
